package com.mysky.client.mvp.audioplayer;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PushButton;
import com.google.inject.Inject;
import com.google.web.bindery.event.shared.EventBus;
import com.gwtplatform.mvp.client.PresenterWidget;
import com.gwtplatform.mvp.client.View;
import com.kiouri.sliderbar.client.view.SliderBar;
import com.mysky.client.mvp.audioplayer.event.MDPauseResumeEvent;
import com.mysky.client.mvp.audioplayer.event.MDSongFinishedEvent;
import com.mysky.client.mvp.audioplayer.event.MDSongPausedEvent;
import com.mysky.client.mvp.audioplayer.event.MDSongStartedEvent;
import com.mysky.client.mvp.audioplayer.soundplayer.SoundPlayer;
import com.mysky.client.resources.images.DefaultIcon;

public class WidgetAudioPresenter extends
		PresenterWidget<WidgetAudioPresenter.MyView> {

	public interface MyView extends View {
		PushButton getPlayButton();

		Label getAudioUrl();

		Label getAudioInfo();

		SliderBar getSliderBar();
	}

	private EventBus eventBus;

	@Inject
	public WidgetAudioPresenter(final EventBus eventBus, final MyView view,
			final SoundPlayer soundPlayer) {
		super(eventBus, view);
		this.soundPlayer = soundPlayer;
		this.eventBus = eventBus;
	}

	SoundPlayer soundPlayer;

	@Override
	protected void onBind() {
		super.onBind();

		handlePlayerEvent();

		getView().getPlayButton().addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				eventBus.fireEvent(new MDPauseResumeEvent());
			}
		});

		soundPlayer.setPlayingCallback(new SoundPlayer.PlayingCallback() {
			public void fire(int now, int end) {
				String total = "" + end / 60 + ":" + end % 60;
				String current = "" + now / 60 + ":" + now % 60;
				int px = getView().getSliderBar().getMaxValue() * now / end;
				getView().getAudioInfo().setText(
						current + " /" + total);
				getView().getSliderBar().setValue(px);
			}
		});

	}

	private void handlePlayerEvent() {
		eventBus.addHandler(MDSongStartedEvent.TYPE,
				new MDSongStartedEvent.MDSongStartedEventHandler() {
					@Override
					public void handleEvent(MDSongStartedEvent event) {
						getView()
								.getPlayButton()
								.getUpFace()
								.setImage(
										new Image(DefaultIcon.INSTANCE
												.MD_pause48x48()));
					}
				});
		eventBus.addHandler(MDSongPausedEvent.TYPE,
				new MDSongPausedEvent.MDSongPausedEventHandler() {
					@Override
					public void handleEvent(MDSongPausedEvent event) {
						getView()
								.getPlayButton()
								.getUpFace()
								.setImage(
										new Image(DefaultIcon.INSTANCE
												.MD_play48x48()));
					}
				});
		eventBus.addHandler(MDSongFinishedEvent.TYPE,
				new MDSongFinishedEvent.MDSongFinishedEventHandler() {
					@Override
					public void handleEvent(MDSongFinishedEvent event) {
						getView()
								.getPlayButton()
								.getUpFace()
								.setImage(
										new Image(DefaultIcon.INSTANCE
												.MD_play48x48()));
					}
				});

	}

	@Override
	protected void onReveal() {
		super.onReveal();
	}
}
